package gui;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import parser.Block;
import parser.FloorPlan;

/**
 * 
 * @author Fozy
 *
 */

public class TreeNodesSchema {

    private ArrayList<Block> blocks;

    public TreeNodesSchema(FloorPlan floorPlan) {
        blocks = new ArrayList<Block>();
        
        for(Block block : floorPlan.getBlocks().values()){
        	addBlock(block);
        }
    }

    public ArrayList<Block> getBlocks() {
        return blocks;
    }

    public void addBlock(Block block) {
        blocks.add(block);
    }

    private double getMinX() {
        double minX = blocks.get(0).getX_coordinate();
        for (Block block : blocks) {
            if (minX > block.getX_coordinate()) {
                minX = block.getX_coordinate();
            }
        }
        return minX;
    }

    private double getWidth() {
        double maxX = blocks.get(0).getX_coordinate();
        for (Block block : blocks) {
            if (maxX < block.getX_coordinate()) {
                maxX = block.getX_coordinate();
            }
        }
        return maxX - getMinX();
    }

    private double getMinY() {
        double minY = blocks.get(0).getY_coordinate();
        for (Block block : blocks) {
            if (minY > block.getY_coordinate()) {
                minY = block.getY_coordinate();
            }
        }
        return minY;
    }

    private double getHeight() {
        double maxY = blocks.get(0).getY_coordinate();
        for (Block block : blocks) {
            if (maxY < block.getY_coordinate()) {
                maxY = block.getY_coordinate();
            }
        }
        return maxY - getMinY();
    }

    public Rectangle getSystemCoordinates() {
        return new Rectangle((int) getMinX(), (int) getMinY(), (int) getWidth(), (int) getHeight());
    }

    public Point placeBlock(Block block, Rectangle limites) {
        double coordinateX = block.getX_coordinate() - 0.5*block.getHorizontalLength() ;
        double coordinateY = block.getY_coordinate() - 0.5*block.getVerticalLength();
        return new Point((int) (20 * (coordinateX)), (int) (20 *( limites.y - coordinateY - block.getVerticalLength())));
    }
}
